<?php
/**
 * HTML Packer SleekPHP Plug-in
 * Packs the entire page before outputting.
 *
 * @subpackage Plugins
 */
Events::bind('BeforeOutput', 'SPHTMLPacker');

/**
 * Packs $html (removes linebreaks and tabs etc)
 *
 * @param string $html
 */
function SPHTMLPacker ($html) {
	$exceptIn = array(
		'<script.*?>'	=> '</script>', 
		'<pre.*?>'		=> '</pre>', 
		'<textarea.*?>'	=> '</textarea>'
	);

	$ignoredBlocks = array();

	# Extract all the parts that we're gonna leave alone
	foreach ($exceptIn as $startTag => $endTag) {
		$pattern = "#$startTag.*?$endTag#s";

		preg_match_all($pattern, $html, $ignoredBlocks[$startTag]);

		$html = preg_replace($pattern, '___[SP_HTML_PACKER_' . $startTag . '_BLOCK]__', $html);
	}

	# Now remove all line-breaks and stuff
	$html = str_replace(array("\n", "\n\r", "\r\n" ,"\r" , "\t"), '', $html);

	# Now re-insert all the ignored blocks
	foreach ($ignoredBlocks as $tag => $matches) {
		foreach ($matches[0] as $block) {
			$html = str_replace_once('___[SP_HTML_PACKER_' . $tag . '_BLOCK]__', $block, $html);
		}
	}

	return $html;
}
